package com.tushare.util;

import com.tushare.entity.Daily;

import java.util.ArrayList;
import java.util.List;

public class CalKdjUtil {

    public static void calKdj(List<Daily> dailyList) {
        List<Float> RSV = new ArrayList<>(dailyList.size());
        Daily tempDaily;
        for (int index = 0; index < dailyList.size(); index++) {
            if (index >= 8) {
                int start = index - 8;
                float high9 = Float.MIN_VALUE;
                float low9 = Float.MAX_VALUE;
                while (start <= index) {
                    tempDaily = dailyList.get(start);
                    high9 = tempDaily.getHigh() > high9 ? tempDaily.getHigh() : high9;
                    low9 = tempDaily.getLow() < low9 ? tempDaily.getLow() : low9;
                    start++;
                }
                RSV.add((dailyList.get(index).getClose() - low9) / (high9 - low9) * 100);
            } else {
                RSV.add(0f);
            }
        }

        List<Float> outSlowK = new ArrayList<>();
        List<Float> outSlowD = new ArrayList<>();
        List<Float> outSlowJ = new ArrayList<>();
        for (int i = 0; i < dailyList.size(); i++) {
            if (i > 1) {
                outSlowK.add(2 / 3f * outSlowK.get(i - 1) + 1 / 3f * RSV.get(i));
                outSlowD.add(2 / 3f * outSlowD.get(i - 1) + 1 / 3f * outSlowK.get(i));
                outSlowJ.add(3 * outSlowK.get(i) - 2 * outSlowD.get(i));
            } else {
                outSlowK.add(50f);
                outSlowD.add(50f);
                outSlowJ.add(50f);
            }
        }
        System.out.println(outSlowK);
        System.out.println(outSlowD);
        System.out.println(outSlowJ);
    }

}
